package org.okcjug.december.map;

import static org.junit.Assert.*;

import java.math.BigDecimal;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.junit.Test;
import org.okcjug.december.set.objects.XmasTree;

public class MyTreeMap
{
	@Test
	public void values()
	{
		Map<String, XmasTree> xmasTrees = new TreeMap<String, XmasTree>();
		xmasTrees.put("Fur", getFurTree());
		xmasTrees.put("Spruce", getSpruceTree());
		xmasTrees.put("Plastic", getPlasticTree());
		
		for(Map.Entry<String, XmasTree> tree : xmasTrees.entrySet()){
			System.out.println("Key: " + tree.getKey());
			System.out.println("Value: " + tree.getValue());
		}
		
		assertTrue(xmasTrees.containsKey("Fur"));
		assertTrue(xmasTrees.containsValue(getFurTree()));
	}
	
	@Test
	public void keys(){
		Map<XmasTree, String> xmasTrees = new TreeMap<XmasTree, String>();
		xmasTrees.put(getFurTree(), "Big spender");
		xmasTrees.put(getSpruceTree(), "Nice choice");
		xmasTrees.put(getPlasticTree(), "Geez, you are cheap");
		
		assertTrue(xmasTrees.containsKey(getFurTree()));
	}
	
	@Test
	public void rangeOperations(){
		TreeMap<BigDecimal, XmasTree> xmasTrees 
			= new TreeMap<BigDecimal, XmasTree>();
		XmasTree fur = getFurTree();
		XmasTree spruce = getSpruceTree();
		XmasTree plastic = getPlasticTree();
		XmasTree shortPlastic = getPlasticTreeShort();
		
		//map keyed off of price
		xmasTrees.put(fur.getPrice(), fur);
		xmasTrees.put(spruce.getPrice(), spruce);
		xmasTrees.put(plastic.getPrice(), plastic);
		xmasTrees.put(shortPlastic.getPrice(), shortPlastic);
		
		BigDecimal nineteenNintyNine = new BigDecimal("19.99");
		BigDecimal sixty = new BigDecimal("60.00");
		
		//floor key example, first key <= given key
		BigDecimal tooCheap
			= xmasTrees.floorKey(nineteenNintyNine);
		assertTrue(tooCheap == null);
		
		//ceilingEntry example,  first key >= given key
		Map.Entry<BigDecimal, XmasTree> justAboveOurOffer 
			= xmasTrees.ceilingEntry(nineteenNintyNine);
		assertNotNull(justAboveOurOffer);
		assertTrue(nineteenNintyNine.compareTo(justAboveOurOffer.getKey()) < 0);
		assertEquals(justAboveOurOffer.getValue(), shortPlastic);
		
		//range example
		SortedMap<BigDecimal, XmasTree> inOurPriceRange 
			= xmasTrees.subMap(nineteenNintyNine, sixty);
		
		for(BigDecimal price : inOurPriceRange.keySet()){
			assertTrue(price.compareTo(nineteenNintyNine) > 0);
			System.out.println("Price: " + price);
			assertTrue(price.compareTo(sixty) < 0);
		}
	}
	
	
	
	
	
	
	private XmasTree getFurTree()
	{
		XmasTree tree = new XmasTree();
		tree.setType("Fur");
		tree.setHeightInInches(72);
		tree.setBaseIncluded(false);
		tree.setFrocked(false);
		tree.setLightsIncluded(false);
		tree.setPrice(new BigDecimal("78.56"));
		return tree;
	}
	private XmasTree getSpruceTree()
	{
		XmasTree tree = new XmasTree();
		tree.setType("Spruce");
		tree.setHeightInInches(60);
		tree.setBaseIncluded(false);
		tree.setFrocked(false);
		tree.setLightsIncluded(false);
		tree.setPrice(new BigDecimal("65.78"));
		return tree;
	}
	private XmasTree getPlasticTree()
	{
		XmasTree tree = new XmasTree();
		tree.setType("Plastic");
		tree.setHeightInInches(48);
		tree.setBaseIncluded(true);
		tree.setFrocked(false);
		tree.setLightsIncluded(true);
		tree.setPrice(new BigDecimal("55.66"));
		return tree;
	}
	private XmasTree getPlasticTreeShort()
	{
		XmasTree tree = new XmasTree();
		tree.setType("Plastic");
		tree.setHeightInInches(32);
		tree.setBaseIncluded(true);
		tree.setFrocked(true);
		tree.setLightsIncluded(true);
		tree.setPrice(new BigDecimal("45.32"));
		return tree;
	}

}
